package http;


import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.ecdc.soexample.App;

import org.json.JSONArray;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import enty.ResultData2;

public class HttpTools {
    private String requestSecurityCodeUrl="http://2et9473959.wicp.vip:52503";//获取验证码
    private String requestCheckSignUpUrl="http://2et9473959.wicp.vip:52503";//对比验证码
    private String requestEmailSignInUrl="http://2et9473959.wicp.vip:52503";//对比验证码





    private HttpToolsCallBack httpToolsCallBack;

    public interface HttpToolsCallBack {
        public void successHttpUtilsCallBack(String data);
        public void errorHttpUtilsCallBack(String data);
    }

    public void setDeligate(HttpToolsCallBack httpToolsCallBack) {
        this.httpToolsCallBack = httpToolsCallBack;

    }


//    public static HttpTools getOtaUtilsInstance() {
//        if (mHttpTools == null) {
//            synchronized (HttpTools.class) {
//                if (mHttpTools == null) {
//                    mHttpTools = new HttpTools();
//                }
//            }
//        }
//        return mHttpTools;
//    }

    /**
     * 获取验证码
     */

    public void getSecurityCode(String email,String emailPassword) {
        // String path="http://192.168.6.195:8080/EasyGo/ecdc/insertDingDan";

        RequestParams params = new RequestParams(requestSecurityCodeUrl);
        params.setConnectTimeout(3000);
        params.addBodyParameter("email", email);
        params.addBodyParameter("emailPassword", emailPassword);
//Callback.CommonCallback<String>()
        x.http ().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                if(httpToolsCallBack!=null){
                    httpToolsCallBack.successHttpUtilsCallBack(result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if(httpToolsCallBack!=null){
                    httpToolsCallBack.errorHttpUtilsCallBack(ex.getMessage());
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    public void getcheckSignUp(String email,String emailPassword,String emailSecurityCode) {
        // String path="http://192.168.6.195:8080/EasyGo/ecdc/insertDingDan";

        RequestParams params = new RequestParams(requestCheckSignUpUrl);
        params.setConnectTimeout(3000);
        params.setMultipart(true);
        params.addBodyParameter("email", email);
        params.addBodyParameter("emailPassword", emailPassword);
        params.addBodyParameter("emailSecurityCode", emailSecurityCode);

        Callback.CommonCallback.Cancelable get = x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(httpToolsCallBack!=null){
                    httpToolsCallBack.successHttpUtilsCallBack(result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if(httpToolsCallBack!=null){
                    httpToolsCallBack.errorHttpUtilsCallBack(ex.getMessage());
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

    public void login(String email,String emailPassword) {
        // String path="http://192.168.6.195:8080/EasyGo/ecdc/insertDingDan";

        RequestParams params = new RequestParams(requestEmailSignInUrl);
        params.setConnectTimeout(3000);
        params.setMultipart(true);
        params.addBodyParameter("email", email);
        params.addBodyParameter("emailPassword", emailPassword);

        Callback.CommonCallback.Cancelable get = x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(httpToolsCallBack!=null){
                    httpToolsCallBack.successHttpUtilsCallBack(result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if(httpToolsCallBack!=null){
                    httpToolsCallBack.errorHttpUtilsCallBack(ex.getMessage());
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }

}
